import util from '@/js/util/util.js';
import { http } from '@/js/util/connect.js';
import message from '@/js/util/message.js';
let editData = {
    init: function(panel) {
        this.createResize(panel);
        this.creatPanel(panel);
    },
    remove: function(panel) {
        this.removePanel(panel);
    },
    creatPanel: function(panel) {
        let current = panel.element;
        if (current.data.type === 'static') {
            panel.data = current.data.value;
            this.setTextData(panel);
        } else {
            this.setRequestData(panel);
        }
        panel.setData = function(data) {
            panel.data = data;
        }
        if (panel.mode === 2) {
            if (current.data.type === 'dynamic') {
                if (current.attribute.interval > 0) {
                    panel.echartTimer = setInterval(() => {
                        this.setRequestData(panel);
                    }, current.attribute.interval)
                }
            }
        }
    },
    removePanel: function(panel) {
        if (panel.echartTimer) {
          clearInterval(panel.echartTimer); // 清除定时器
        }
    },
    setRequestData: function(panel) {
        try {
            let data = http(panel.element.data.url, {}, panel.element.data.method === '0' ? 'GET' : 'POST');
            panel.data = this.transferData(data, panel.element.data.key);
            this.setTextData(panel);
        } catch (error) { }
    },
    transferData: function(interfaceData, key) {
        let interfaceGridData = [];
        let list = this.getData(interfaceData, key);
        if (Object.prototype.toString.call(list) === '[object Object]') {
            list = [list]
        }
        let headerObject = {};
        let headerList = [];
        for (let a in list) {
        for (let b in list[a]) {
            if (headerObject[b] != true) {
            headerList.push(b);
            headerObject[b] = true;
            }
        }
        }
        interfaceGridData.push(headerList);
        for (let a = 0; a < list.length; a ++) {
        interfaceGridData.push([]);
        for (let i = 0; i < headerList.length; i ++) {
            let value = list[a][headerList[i]];
            if (value == null || value == undefined) {
            value = '';
            }
            interfaceGridData[a + 1].push(value);
        }
        }
        return interfaceGridData;
    },
    getData: function(data, array) {
        let value = data;
        for (let index = 0; index < array.length; index++) {
          value = value[array[index]];
        }
        if (value == data) {
          return null;
        } else {
          return value;
        }
    },
    setTextData: function(panel) {
        let current = panel.element;
        let component = current.component;
        switch (component) {
            case 'Statistic05': {
                let len = current.data.field.length - 1;
    
                let categoryIndex = +current.data.field[0].index;
                let principalAxis = [];
                for (let i = 0; i < panel.data.length - 1; i++) {
                    let element = panel.data[i + 1][categoryIndex];
                    principalAxis.push(element);
                };
    
                let valueAggregate = [];
                for (let index = 0; index < len; index++) {
                    valueAggregate[index] = [];
                    let fieldsIndex = +current.data.field[index + 1].index;
                    for (let j = 0; j < panel.data.length - 1; j++) {
                        let element = panel.data[j + 1][fieldsIndex];
                        valueAggregate[index].push(+element);
                    }
                };
                current.data.text = principalAxis.join(',') + valueAggregate.join(',');
                break;
            }
            default:
                break;
        }
    },
    // download 导出 二维数组转化
    getDownload: function(values) {
        let data = values;
        let result = [];
        for (let index = 0; index < data.length; index++) {
            let element = data[index];
            let obj = {};
            for (let i = 0; i < element.length; i++) {
                let el = element[i];
                obj[i] = el;
            }
            result.push(obj);
        };
        return result;
    },
    echartTimer: {},
    createResize: function(panel) {
        let _this = this;
        panel.redraw = function(e) {
            let resize = function() {
                _this.setTextData(panel);
                delete _this.echartTimer['timer_' + util.replaceMinus(panel.id)];
            };
            if (e && e.deltay) {
                let timer = _this.echartTimer['timer_' + util.replaceMinus(panel.id)];
                if (timer) {
                    clearTimeout(timer);
                    _this.echartTimer['timer_' + util.replaceMinus(panel.id)] = null;
                }
                _this.echartTimer['timer_' + util.replaceMinus(panel.id)] = window.setTimeout(resize, e.deltay);
            } else {
                resize();
            }
        };
    },
}
export default editData;